#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::initEnvironment()
{
    //初始化CAD环境，版本AutoCAD18
    pAcad = new QAxObject("AutoCAD.Application");
    pAcad->setProperty("Visible",true);
}
void MainWindow::fillParam()
{
    QCheckBox* idcIsExport = this->findChild<QCheckBox*>("IDC_IS_EXPORT");
    QComboBox* idcWndType = this->findChild<QComboBox*>("IDC_WINDOW_TYPE");
    QComboBox* exportPath  = this->findChild<QComboBox*>("IDC_EXPORT_PATH");

    //非文本框
    tableData->find("IDC_WINDOW_TYPE")->second = QString(idcWndType->currentIndex());
    tableData->find("IDC_IS_EXPORT")->second = idcIsExport->isChecked() ? "1" : "0";
    tableData->find("IDC_EXPORT_PATH")->second = idcIsExport->isChecked() ?  exportPath->currentText():"";

    for(auto i : *qLineMap)
    {
        tableData->find(i.first)->second = i.second->text();
    }
}
void MainWindow::initComponment()
{
        //杂项，非文本框
        tableData->insert(std::make_pair("IDC_WINDOW_TYPE","-1"));
        tableData->insert(std::make_pair("IDC_IS_EXPORT","0"));
        tableData->insert(std::make_pair("IDC_EXPORT_PATH","NULL"));

        //填充文本框和参数
        QList<QLineEdit*> editList = this->findChildren<QLineEdit*>();
        for(QLineEdit* edit: editList)
        {
            edit->setText("0");
            //edit->setEnabled(false);
            qLineMap->insert(std::make_pair(edit->objectName(),edit));
            tableData->insert(std::make_pair(edit->objectName(),"0.0"));
        }
        //填充其余内容
        QPushButton* exportPathBrowse = this->findChild<QPushButton*>("IDC_EXPORT_PATH_BROWSE");
        QComboBox* exportPath  = this->findChild<QComboBox*>("IDC_EXPORT_PATH");
        QCheckBox* isExport = this->findChild<QCheckBox*>("IDC_IS_EXPORT");
        if (isExport == NULL || exportPath == NULL || exportPathBrowse == NULL) return;
        isExport->setCheckState(Qt::CheckState::Unchecked);
        exportPath->setEditText("");
        exportPath->setEnabled(false);
        exportPathBrowse->setEnabled(false);
}
std::map<QString, QString>* MainWindow::getTableData()
{
    return this->tableData;
}
void MainWindow::drawCADGraphic()
{
    fillParam();

    qDebug() << (*(tableData->find("IDC_WINDOW_TYPE"))).second;
    int wndType = (*(tableData->find("IDC_WINDOW_TYPE"))).second.toInt();

    DrawWindow* dw = new DrawWindow(pAcad,wndType,tableData);
    qDebug() << wndType;
    dw->Draw();
    qDebug() << "drawing finished";
    delete dw;

}
 bool MainWindow::checkText()
 {
     QList<QLineEdit*> editList = this->findChildren<QLineEdit*>();
     for(QLineEdit* edit: editList)
     {
            QString estr = edit->text();
            QVariant varia(estr);
            if(!(varia.canConvert(QVariant::Int) || varia.canConvert(QVariant::Double)))
            {
                return false;
            }
     }
     return true;
 }
 void MainWindow::changeWnd(int wndType)
 {
        initComponment();
        qDebug() << QString::number(wndType);
        tableData->find("IDC_WINDOW_TYPE")->second = QString::number(wndType);
        tableData->insert(std::make_pair("IDC_WINDOW_TYPE", QString::number(wndType)));
        qDebug() << *(tableData->find("IDC_WINDOW_TYPE"));

        QLineEdit* UpperLBoardNum  = qLineMap->find("IDC_UPPER_LBOARD_NUM")->second;
                   QLineEdit* LowerLBoardNum  = qLineMap->find("IDC_LOWER_LBOARD_NUM")->second;
                   QLineEdit* LeftLBoardNum  = qLineMap->find("IDC_LEFT_LBOARD_NUM")->second;
                   QLineEdit* RightLBoardNum  = qLineMap->find("IDC_RIGHT_LBOARD_NUM")->second;
                   QLineEdit* PlainEdgeNum  = qLineMap->find("IDC_PLAIN_EDGE_NUM")->second;
                   QLineEdit* HidePlainEdgeNum  = qLineMap->find("IDC_HIDE_PLAIN_EDGE_NUM")->second;
                   QLineEdit* HideCoverEdgeNum  = qLineMap->find("IDC_HIDE_COVER_EDGE_NUM")->second;
                   QLineEdit* PlainCoverEdgeNum  = qLineMap->find("IDC_PLAIN_COVER_EDGE_NUM")->second;
                   QLineEdit* UpperBoardNum  = qLineMap->find("IDC_UPPER_BOARD_NUM")->second;
                   QLineEdit* LowerBoardNum  = qLineMap->find("IDC_LOWER_BOARD_NUM")->second;
                   QLineEdit* BladeNum  = qLineMap->find("IDC_BLADE_NUM")->second;
                   QLineEdit* InvisPullRod1Num  = qLineMap->find("IDC_INVIS_PULL_ROD1_NUM")->second;
                   QLineEdit* InvisPullRod2Num  = qLineMap->find("IDC_INVIS_PULL_ROD2_NUM")->second;

                   QLineEdit* UpperLBoardLength  = qLineMap->find("IDC_UPPER_LBOARD_LENGTH")->second;
                   QLineEdit* LowerLBoardLength  = qLineMap->find("IDC_LOWER_LBOARD_LENGTH")->second;
                   QLineEdit* LeftLBoardLength  = qLineMap->find("IDC_LEFT_LBOARD_LENGTH")->second;
                   QLineEdit* RightLBoardLength  = qLineMap->find("IDC_RIGHT_LBOARD_LENGTH")->second;
                   QLineEdit* PlainEdgeLength  = qLineMap->find("IDC_PLAIN_EDGE_LENGTH")->second;
                   QLineEdit* HidePlainEdgeLength  = qLineMap->find("IDC_HIDE_PLAIN_EDGE_LENGTH")->second;
                   QLineEdit* HideCoverEdgeLength  = qLineMap->find("IDC_HIDE_COVER_EDGE_LENGTH")->second;
                   QLineEdit* PlainCoverEdgeLength  = qLineMap->find("IDC_PLAIN_COVER_EDGE_LENGTH")->second;
                   QLineEdit* UpperBoardLength  = qLineMap->find("IDC_UPPER_BOARD_LENGTH")->second;
                   QLineEdit* LowerBoardLength  = qLineMap->find("IDC_LOWER_BOARD_LENGTH")->second;
                   QLineEdit* BladeLength  = qLineMap->find("IDC_BLADE_LENGTH")->second;
                   QLineEdit* InvisPullRod1Length  = qLineMap->find("IDC_INVIS_PULL_ROD1_LENGTH")->second;
                   QLineEdit* InvisPullRod2Length  = qLineMap->find("IDC_INVIS_PULL_ROD2_LENGTH")->second;

                   QLineEdit* UpperLBoardWidth  = qLineMap->find("IDC_UPPER_LBOARD_WIDTH")->second;
                   QLineEdit* LowerLBoardWidth  = qLineMap->find("IDC_LOWER_LBOARD_WIDTH")->second;
                   QLineEdit* LeftLBoardWidth  = qLineMap->find("IDC_LEFT_LBOARD_WIDTH")->second;
                   QLineEdit* RightLBoardWidth  = qLineMap->find("IDC_RIGHT_LBOARD_WIDTH")->second;
                   QLineEdit* PlainEdgeWidth  = qLineMap->find("IDC_PLAIN_EDGE_WIDTH")->second;
                   QLineEdit* HidePlainEdgeWidth  = qLineMap->find("IDC_HIDE_PLAIN_EDGE_WIDTH")->second;
                   QLineEdit* HideCoverEdgeWidth  = qLineMap->find("IDC_HIDE_COVER_EDGE_WIDTH")->second;
                   QLineEdit* PlainCoverEdgeWidth  = qLineMap->find("IDC_PLAIN_COVER_EDGE_WIDTH")->second;
                   QLineEdit* UpperBoardWidth  = qLineMap->find("IDC_UPPER_BOARD_WIDTH")->second;
                   QLineEdit* LowerBoardWidth  = qLineMap->find("IDC_LOWER_BOARD_WIDTH")->second;
                   QLineEdit* BladeWidth  = qLineMap->find("IDC_BLADE_WIDTH")->second;
                   QLineEdit* InvisPullRod1Width  = qLineMap->find("IDC_INVIS_PULL_ROD1_WIDTH")->second;
                   QLineEdit* InvisPullRod2Width  = qLineMap->find("IDC_INVIS_PULL_ROD2_WIDTH")->second;

                   QLineEdit* UpperLBoardHeight  = qLineMap->find("IDC_UPPER_LBOARD_HEIGHT")->second;
                   QLineEdit* LowerLBoardHeight  = qLineMap->find("IDC_LOWER_LBOARD_HEIGHT")->second;
                   QLineEdit* LeftLBoardHeight  = qLineMap->find("IDC_LEFT_LBOARD_HEIGHT")->second;
                   QLineEdit* RightLBoardHeight  = qLineMap->find("IDC_RIGHT_LBOARD_HEIGHT")->second;
                   QLineEdit* PlainEdgeHeight  = qLineMap->find("IDC_PLAIN_EDGE_HEIGHT")->second;
                   QLineEdit* HidePlainEdgeHeight  = qLineMap->find("IDC_HIDE_PLAIN_EDGE_HEIGHT")->second;
                   QLineEdit* HideCoverEdgeHeight  = qLineMap->find("IDC_HIDE_COVER_EDGE_HEIGHT")->second;
                   QLineEdit* PlainCoverEdgeHeight  = qLineMap->find("IDC_PLAIN_COVER_EDGE_HEIGHT")->second;
                   QLineEdit* UpperBoardHeight  = qLineMap->find("IDC_UPPER_BOARD_HEIGHT")->second;
                   QLineEdit* LowerBoardHeight  = qLineMap->find("IDC_LOWER_BOARD_HEIGHT")->second;
                   QLineEdit* BladeHeight  = qLineMap->find("IDC_BLADE_HEIGHT")->second;
                   QLineEdit* InvisPullRod1Height  = qLineMap->find("IDC_INVIS_PULL_ROD1_HEIGHT")->second;
                   QLineEdit* InvisPullRod2Height  = qLineMap->find("IDC_INVIS_PULL_ROD2_HEIGHT")->second;

                   //QLineEdit* UpperLBoardSlotWidth  = qLineMap->find("IDC_UPPER_LBOARD_SLOT_WIDTH")->second;
                   //QLineEdit* LowerLBoardSlotWidth = qLineMap->find("IDC_LOWER_LBOARD_SLOT_WIDTH")->second;
                   //QLineEdit* LeftLBoardSlotWidth  = qLineMap->find("IDC_LEFT_LBOARD_SLOT_WIDTH")->second;
                   //QLineEdit* RightLBoardSlotWidth  = qLineMap->find("IDC_RIGHT_LBOARD_SLOT_WIDTH")->second;
                   QLineEdit* HidePlainEdgeSlotWidth  = qLineMap->find("IDC_HIDE_PLAIN_EDGE_SLOT_WIDTH")->second;
                   //QLineEdit* UpperLBoardSlotDept  = qLineMap->find("IDC_UPPER_LBOARD_SLOT_DEPT")->second;
                   //QLineEdit* LowerLBoardSlotDept = qLineMap->find("IDC_LOWER_LBOARD_SLOT_DEPT")->second;
                   //QLineEdit* LeftLBoardSlotDept  = qLineMap->find("IDC_LEFT_LBOARD_SLOT_DEPT")->second;
                   //QLineEdit* RightLBoardSlotDept  = qLineMap->find("IDC_RIGHT_LBOARD_SLOT_DEPT")->second;
                   QLineEdit* HidePlainEdgeSlotDept  = qLineMap->find("IDC_HIDE_PLAIN_EDGE_SLOT_DEPT")->second;
                   QLineEdit* HideCoverEdgeCoverMaxWidth  = qLineMap->find("IDC_HIDE_COVER_EDGE_COVER_MAX_WIDTH")->second;
                   QLineEdit* HideCoverEdgeCoverMaxHeight  = qLineMap->find("IDC_HIDE_COVER_EDGE_COVER_MAX_HEIGHT")->second;
                   //QLineEdit* HideCoverEdgeCoverBumpLength  = qLineMap->find("IDC_HIDE_COVER_EDGE_COVER_BUMP_LENGTH")->second;
                   QLineEdit* PlainCoverEdgeCoverMaxWidth  = qLineMap->find("IDC_PLAIN_COVER_EDGE_COVER_MAX_WIDTH")->second;
                   QLineEdit* PlainCoverEdgeCoverMaxHeight  = qLineMap->find("IDC_PLAIN_COVER_EDGE_COVER_MAX_HEIGHT")->second;
                   //QLineEdit* PlainCoverEdgeCoverBumpLength  = qLineMap->find("IDC_PLAIN_COVER_EDGE_COVER_BUMP_LENGTH")->second;

                   QLineEdit* LBoardSlotWidth  = qLineMap->find("IDC_LBOARD_SLOT_WIDTH")->second;
                   //QLineEdit* LowerBoardSlotWidth  = qLineMap->find("IDC_LOWER_BOARD_SLOT_WIDTH")->second;
                   QLineEdit* LBoardSlotDept  = qLineMap->find("IDC_LBOARD_SLOT_DEPT")->second;
                   //QLineEdit* LowerBoardSlotDept  = qLineMap->find("IDC_LOWER_BOARD_SLOT_DEPT")->second;
                   QLineEdit* HingeLength = qLineMap->find("IDC_HINGE_LENGTH")->second;
                   QLineEdit* HingeWidth = qLineMap->find("IDC_HINGE_WIDTH")->second;
                   QLineEdit* HingeNum = qLineMap->find("IDC_HINGE_NUM")->second;
                   QLineEdit* LBoardLHeight = qLineMap->find("IDC_LBOARD_LHEIGHT")->second;

                   QLineEdit* windowNum = qLineMap->find("IDC_WINDOW_NUM")->second;
                   QLineEdit* windowWidth = qLineMap->find("IDC_WINDOW_WIDTH")->second;
                   //20250325
                   QLineEdit* HideCoverEdgerCoverWidth = qLineMap->find("IDC_HIDE_COVER_EDGE_COVER_WIDTH")->second;
                   QLineEdit* PlainCoverEdgerCoverWidth = qLineMap->find("IDC_PLAIN_COVER_EDGE_COVER_WIDTH")->second;

         switch (wndType)
         {
         case 0:
             UpperLBoardNum->setText("1");
             LeftLBoardNum->setText("1");
             RightLBoardNum->setText("1");
             PlainEdgeNum->setText("1");
             HidePlainEdgeNum->setText("1");
             UpperBoardNum->setText("1");
             LowerBoardNum->setText("1");
             BladeNum->setText("10");
             InvisPullRod1Num->setText("1");
             HingeNum->setText("2");
             windowNum->setText("1");

             UpperLBoardLength->setText("600");
             UpperLBoardWidth->setText("36.21");
             UpperLBoardHeight->setText("53.5");
             LeftLBoardLength->setText("690");
             LeftLBoardWidth->setText("36.21");
             LeftLBoardHeight->setText("53.5");
             RightLBoardLength->setText("690");
             RightLBoardWidth->setText("36.21");
             RightLBoardHeight->setText("53.5");
             PlainEdgeLength->setText("663.23");
             PlainEdgeWidth->setText("45");
             PlainEdgeHeight->setText("27");
             HidePlainEdgeLength->setText("663.23");
             HidePlainEdgeWidth->setText("45");
             HidePlainEdgeHeight->setText("27");
             HidePlainEdgeSlotDept->setText("15");
             HidePlainEdgeSlotWidth->setText("18");
             UpperBoardLength->setText("511.4");
             UpperBoardWidth->setText("77.61");
             UpperBoardHeight->setText("19.05");
             LowerBoardLength->setText("511.4");
             LowerBoardWidth->setText("77.61");
             LowerBoardHeight->setText("19.05");
             BladeLength->setText("458.7");
             BladeWidth->setText("63.5");
             BladeHeight->setText("11.1");
             InvisPullRod1Height->setText("477.2");
             InvisPullRod1Width->setText("6");
             InvisPullRod1Length->setText("12.5");
             LBoardSlotWidth->setText("5.0");
             LBoardSlotDept->setText("9.0");
             LBoardLHeight->setText("24.92");
             break;
         case 1:
             UpperLBoardNum->setText("1");
             LowerLBoardNum->setText("1");
             LeftLBoardNum->setText("1");
             RightLBoardNum->setText("1");
             PlainEdgeNum->setText("1");
             HidePlainEdgeNum->setText("2");
             HideCoverEdgeNum->setText("1");
             UpperBoardNum->setText("2");
             LowerBoardNum->setText("2");
             BladeNum->setText("36");
             InvisPullRod1Num->setText("4");
             HingeNum->setText("4");
             windowNum->setText("2");
//             tableData->insert(std::make_pair("IDC_UPPER_LBOARD_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_LOWER_LBOARD_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_LEFT_LBOARD_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_RIGHT_LBOARD_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_HIDE_PLAIN_EDGE_NUM", "2"));
//             tableData->insert(std::make_pair("IDC_PLAIN_EDGE_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_HIDE_COVER_EDGE_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_UPPER_BOARD_NUM", "2"));
//             tableData->insert(std::make_pair("IDC_LOWER_BOARD_NUM", "2"));
//             tableData->insert(std::make_pair("IDC_BLADE_NUM", "36"));
//             tableData->insert(std::make_pair("IDC_INVIS_PULL_ROD1_NUM", "4"));
//              tableData->insert(std::make_pair("IDC_HINGE_NUM","4"));
//              tableData->insert(std::make_pair("IDC_WINDOW_NUM","2"));
             break;
         case 2:
             UpperLBoardNum->setText("1");
             LowerLBoardNum->setText("1");
             LeftLBoardNum->setText("1");
             RightLBoardNum->setText("1");
             PlainEdgeNum->setText("2");
             HidePlainEdgeNum->setText("2");
             HideCoverEdgeNum->setText("1");
             PlainCoverEdgeNum->setText("1");
             UpperBoardNum->setText("3");
             LowerBoardNum->setText("3");
             BladeNum->setText("51");
             InvisPullRod1Num->setText("4");
             InvisPullRod2Num->setText("5");
             HingeNum->setText("6");
             windowNum->setText("3");
//             tableData->insert(std::make_pair("IDC_UPPER_LBOARD_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_LOWER_LBOARD_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_LEFT_LBOARD_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_RIGHT_LBOARD_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_HIDE_PLAIN_EDGE_NUM", "2"));
//             tableData->insert(std::make_pair("IDC_PLAIN_EDGE_NUM", "2"));
//             tableData->insert(std::make_pair("IDC_HIDE_COVER_EDGE_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_PLAIN_COVER_EDGE_NUM", "1"));
//             tableData->insert(std::make_pair("IDC_UPPER_BOARD_NUM", "3"));
//             tableData->insert(std::make_pair("IDC_LOWER_BOARD_NUM", "3"));
//             tableData->insert(std::make_pair("IDC_BLADE_NUM", "51"));
//             tableData->insert(std::make_pair("IDC_INVIS_PULL_ROD1_NUM", "4"));
//             tableData->insert(std::make_pair("IDC_INVIS_PULL_ROD2_NUM", "5"));
//              tableData->insert(std::make_pair("IDC_HINGE_NUM","6"));
//              tableData->insert(std::make_pair("IDC_WINDOW_NUM","3"));
             break;
         default:
             break;
         }
 }
void MainWindow::findAndConnect()
{
    QCheckBox* idcIsExport = this->findChild<QCheckBox*>("IDC_IS_EXPORT");
    QPushButton* idcExportBrowser = this->findChild<QPushButton*>("IDC_EXPORT_PATH_BROWSE");
    QPushButton* idcExportPreview = this->findChild<QPushButton*>("IDC_EXPORT_PREVIEW");
    QPushButton* idcExport = this->findChild<QPushButton*>("IDC_EXPORT");
    QComboBox* idcWndType = this->findChild<QComboBox*>("IDC_WINDOW_TYPE");
    connect(idcIsExport,&QCheckBox::stateChanged,this,&MainWindow::enableOrDiasbleIdcExport);
    connect(idcExportBrowser,&QPushButton::clicked,this,&MainWindow::configExportFilePath);
    connect(idcWndType,(void (QComboBox::*)(int))&QComboBox::currentIndexChanged,this,&MainWindow::switchWindowType);
    connect(idcExportPreview,&QPushButton::clicked,this,&MainWindow::drawPreviewGraphic);
    connect(idcExport,&QPushButton::clicked,this,&MainWindow::exportFile);
}
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initEnvironment();
    initComponment();
    findAndConnect();
}

MainWindow::~MainWindow()
{
    pAcad->dynamicCall("Quit()");
    delete ui;
}

